﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using shenkong.Models;

namespace shenkong.Controllers
{
    public class RepairRecordsController : Controller
    {
        private readonly dormitoryContext _context;

        public RepairRecordsController(dormitoryContext context)
        {
            _context = context;
        }

        // GET: RepairRecords
        [Authorize]
        public async Task<IActionResult> Index()
        {

            //var s = _context.RepairRecords.Where(r => r.Zhuangtai == null).ToList();
            //状态为null的记录
            var records = _context.RepairRecords.Where(r => r.Zhuangtai == null).ToList();

            //已受理的记录
            var orderRecords = _context.RepairRecords.Where(r => r.Zhuangtai == "已受理").ToList();

            var model = new Dictionary<string, List<RepairRecord>>();
            model["NullStatus"] = records;
            model["OtherStatus"] = orderRecords;

            return View(model);
        }

        // GET: RepairRecords/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null || _context.RepairRecords == null)
            {
                return NotFound();
            }

            var repairRecord = await _context.RepairRecords
                .FirstOrDefaultAsync(m => m.RepairId == id);
            if (repairRecord == null)
            {
                return NotFound();
            }

            return View(repairRecord);
        }

        // GET: RepairRecords/Create
        //[Authorize]
        public IActionResult Create()
        {
            return View();
        }

        // POST: RepairRecords/Create
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("RepairId,BuildName,ApplyDate,DormId,WorkId,RepairProblem,Tel,Zhuangtai,RepairClass,FreeTime,Replyer")] RepairRecord repairRecord)
        {
            
            if (ModelState.IsValid)
            {
                _context.Add(repairRecord);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(back));
            }
            return View(repairRecord);
        }

        // GET: RepairRecords/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null || _context.RepairRecords == null)
            {
                return NotFound();
            }

            var repairRecord = await _context.RepairRecords.FindAsync(id);
            if (repairRecord == null)
            {
                return NotFound();
            }
        
            return View(repairRecord);
        }

        // POST: RepairRecords/Edit/5
        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
        [Authorize]
        [HttpPost]
        [ValidateAntiForgeryToken]
        //public async Task<IActionResult> Edit(int id, [Bind("RepairId,BuildName,ApplyDate,DormId,WorkId,RepairProblem,Tel,Zhuangtai,RepairClass,FreeTime,Replyer")] RepairRecord repairRecord)
        public async Task<IActionResult> Edit(int id, [Bind("WorkId,Zhuangtai")] RepairRecord repairRecord)
        {
            //if (id != repairRecord.RepairId)
            //{
            //    return Content("没找到");
            //}

            //if (ModelState.IsValid)
            //{
            //    try
            //    {
            //        _context.Update(repairRecord);
            //        await _context.SaveChangesAsync();
            //    }
            //    catch (DbUpdateConcurrencyException)
            //    {
            //        if (!RepairRecordExists(repairRecord.RepairId))
            //        {
            //            return NotFound();
            //        }
            //        else
            //        {
            //            throw;
            //        }
            //    }
            //    return RedirectToAction(nameof(Index));
            //}
            var wordId = repairRecord.WorkId;
            var status = repairRecord.Zhuangtai;
            //_context.Update(repairRecord);
            //await _context.SaveChangesAsync();
            var repair = _context.RepairRecords.FirstOrDefault(s => s.RepairId == id);
            if (repair == null)
            {
                return NotFound();
            }
            repair.Zhuangtai = status;
            repair.WorkId = wordId;
            _context.Update(repair);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        // GET: RepairRecords/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null || _context.RepairRecords == null)
            {
                return NotFound();
            }

            var repairRecord = await _context.RepairRecords
                .FirstOrDefaultAsync(m => m.RepairId == id);
            if (repairRecord == null)
            {
                return NotFound();
            }

            return View(repairRecord);
        }

        public IActionResult back()
        {
            return View();
        }

        // POST: RepairRecords/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            if (_context.RepairRecords == null)
            {
                return Problem("Entity set 'dormitoryContext.RepairRecords'  is null.");
            }
            var repairRecord = await _context.RepairRecords.FindAsync(id);
            if (repairRecord != null)
            {
                _context.RepairRecords.Remove(repairRecord);
            }
            
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool RepairRecordExists(int id)
        {
          return (_context.RepairRecords?.Any(e => e.RepairId == id)).GetValueOrDefault();
        }
    }
}
